package com.weavernorth.humanresource.regular;

import com.weavernorth.humanresource.SalarRangeUtil;
import com.weavernorth.humanresource.personnelBecomeRegularWorker;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import weaver.workflow.webservices.WorkflowRequestInfo;
import weaver.workflow.webservices.WorkflowServiceImpl;

import java.util.Iterator;

/**
 * 在线自定义action接口
 */
public class RegularDeal extends BaseBean {
    public static Log newlog = LogFactory.getLog(RegularDeal.class.getName());

    /**
     * 转正流程第二个节点自动提交
     */

    public String getRegular(String strRequestid,int strUserid) {

        //修改流程数据后自动提交下一节点功能start
        newlog.info("requestid" + strRequestid + "流程后更新自动转下一节点开始");
        RecordSet recordSet = new RecordSet();
        String strsql = "select creater from workflow_requestbase where requestid=?";
        recordSet.executeQuery(strsql, strRequestid);
        newlog.info("strsql为" + strsql);

        WorkflowServiceImpl workflowService = new WorkflowServiceImpl();
        //获取流程信息
        WorkflowRequestInfo wri = workflowService.getWorkflowRequest(Util.getIntValue(strRequestid, 0), strUserid, strUserid);
        //提交流程后返回结果
        String strResult = workflowService.submitWorkflowRequest(wri, Util.getIntValue(strRequestid, 0), strUserid, "submit",
                null);
        newlog.info("requestid" + strRequestid + "流程退回后更新自动转下一节点结束");
        newlog.info("返回结果:" + strResult);
        //修改流程数据后自动提交下一节点功能end
        return strResult;

    }

    //转正提交后,推送SAP
    public String sendSAP(String strRequestid, String strTable) {
        String strFhz = "";
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        //员工编号
        String strYgbh = "";
        //转正日期
        String strZzzrq = "";
        //操作类型
        String strCzlx = "Z2";
        //操作原因
        String strCzyy = "";
        //岗位
        String strGw = "";
        //人事范围
        String strRsfw = "";
        //员工组
        String strYgz = "";
        //员工子组
        String strYgzz = "";
        //人事子范围
        String strRszfw = "";
        //工资核算范围
        String strGzfw = "";
        String sql = " select ygbh,zzrq,czyy,gw,rsfw,ygz,ygzz,rszfw,gzfw from " + strTable + " where requestid =" + strRequestid;
        rs.execute(sql);
        while (rs.next()) {
            //员工编号
            strYgbh = rs.getString("ygbh");
            //转正日期
            strZzzrq = rs.getString("zzrq");

            switch (rs.getString("czyy")) {
                case "0":
                    strCzyy = "10";
                    break;
                case "1":
                    strCzyy = "20";
                    break;
                default:
                    strCzyy = "";
            }
            //岗位
            strGw = rs.getString("gw");
            //人事范围
            strRsfw = rs.getString("rsfw");
            //员工组
            strYgz = rs.getString("ygz");
            //员工子组
            strYgzz = rs.getString("ygzz");
            //人事子范围
            strRszfw = rs.getString("rszfw");
            //工资核算范围
            strGzfw = rs.getString("gzfw");
        }
        sql = "select jobtitlecode from hrmjobtitles where jobstatus ='0' and id = '" + strGw+"'";
        rs.execute(sql);
        while (rs.next()) {
            //岗位
            strGw = rs.getString("jobtitlecode");
        }
        sql = "select rsfwdm from uf_rsfw where id = '" + strRsfw+"'";
        rs.execute(sql);
        while (rs.next()) {
            //人事范围
            strRsfw = rs.getString("rsfwdm");
        }
        sql = "select ygzdm from uf_ygz where id = '" + strYgz+"'";
        rs.execute(sql);
        while (rs.next()) {
            //员工组
            strYgz = rs.getString("ygzdm");
        }
        sql = "select ygzzdm from uf_ygzz where id = '" + strYgzz+"'";
        rs.execute(sql);
        while (rs.next()) {
            //员工子组
            strYgzz = rs.getString("ygzzdm");
        }
        sql = "select rszfwdm from uf_rszfw where id = '" + strRszfw+"'";
        rs.execute(sql);
        while (rs.next()) {
            //人事子范围
            strRszfw = rs.getString("rszfwdm");
        }
        sql = "select gzhsfw from uf_gzfw where id = '" + strGzfw+"'";
        rs.execute(sql);
        while (rs.next()) {
            //工资核算范围
            strGzfw = rs.getString("gzhsfw");
        }
        StringBuffer xml = new StringBuffer("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">");
        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body><urn:ZHRI_003>");
        xml.append("<I_ABKRS>" + strGzfw + "</I_ABKRS>");
        xml.append("<I_BEGDA>" + strZzzrq + "</I_BEGDA>");
        xml.append("<I_BTRTL>" + strRszfw + "</I_BTRTL>");
        xml.append("<I_MASSG>" + strCzyy + "</I_MASSG>");
        xml.append("<I_MASSN>" + strCzlx + "</I_MASSN>");
        xml.append("<I_PERNR>" + strYgbh + "</I_PERNR>");
        xml.append("<I_PERSG>" + strYgz + "</I_PERSG>");
        xml.append("<I_PERSK>" + strYgzz + "</I_PERSK>");
        xml.append("<I_PLANS>" + strGw + "</I_PLANS>");
        xml.append("<I_WERKS>" + strRsfw + "</I_WERKS>");
        xml.append("<RETURN><item>");
        xml.append("<TYPE></TYPE>");
        xml.append("<ID></ID>");
        xml.append("<NUMBER></NUMBER>");
        xml.append("<MESSAGE></MESSAGE>");
        xml.append("<LOG_NO></LOG_NO>");
        xml.append("<LOG_MSG_NO></LOG_MSG_NO>");
        xml.append("<MESSAGE_V1></MESSAGE_V1><MESSAGE_V2></MESSAGE_V2><MESSAGE_V3></MESSAGE_V3><MESSAGE_V4></MESSAGE_V4>");
        xml.append("<PARAMETER></PARAMETER>");
        xml.append("<ROW></ROW>");
        xml.append("<FIELD></FIELD>");
        xml.append("<SYSTEM></SYSTEM>");
        xml.append("</item></RETURN>");
        xml.append("</urn:ZHRI_003></soapenv:Body>");
        xml.append("</soapenv:Envelope>");
        newlog.info("zhuanzhengchuanshudexmlis:" + xml.toString());
        try {
            SalarRangeUtil salarRangeUtil=new SalarRangeUtil();
            //获取配置文件信息
            String strZzUrl = bb.getPropValue("personnel-erdos", "zzurl");
            String responseXml = salarRangeUtil.sendPost(strZzUrl, xml.toString(), "utf-8");
            //解析返回值开始
            Document doc = null;
            try {
                doc = DocumentHelper.parseText(responseXml);
                Iterator<Element> its2 = doc.selectNodes("//TYPE").iterator();
                while (its2.hasNext()) {
                    Element element = its2.next();
                    strFhz = element.getText();
                    newlog.info("当前返回标识:"+strFhz);
                    if (!"".equals(strFhz))
                        break;
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }//解析结束
        } catch (Exception e) {
            e.printStackTrace();
        }
        //推送SAP成功后更新表单字段

        if ("S".equals(strFhz)) {
            String strUpdate = "update " + strTable + " set sftssap='0' where requestid=?";
            rs.executeUpdate(strUpdate, strRequestid);
            return "success";
        } else {
            return "fail";
        }


    }
}
